/**
 * Method that get blog users list from server and display in 
 * the drop-down list, clean existing blogs if selection changes
 * @method getBlogs
 */
function getBlogs() {
	$.ajax({
		url:"scripts/proxyBlog.cgi",
		type:"get",
		data:"method=getBlogs&type=json",
		dataType:"json",
		success:function(data) {
			for(var i in data.Blogs) {
				var blog_user = data.Blogs[i];
/*				
				var option = jQuery("new option");
				$("#blog_users").append(option);
				$(option).attr("value", blog_user);
				if(blog_user == "51158405") {
					$(option).attr("selected", "true");
				}
				
				if(blog_user=="51158405"){
					$("#blog_users").append($("<option></option>").val(blog_user).html(blog_user).attribute("selected", "true") );

				}
*/				
					$("#blog_users").append($("<option></option>").val(blog_user).html(blog_user));
							
			}
		},
		error:function(h) {
			$("#blog_users").append("<option value=''>Error in retrieving blog users</option>");
		}
	});
	
	// when select a user, clean the page and show content accordingly
	$("#blog_users").change(function(){
		$("#content").html("");
		getPosts($('#blog_users').val());
	});
}

/**
 * Method that get all posts of a user from the server and display on the screen
 * @method getPost
 * @param {String}author The id of blog author
 */
function getPosts(author) {
	$.ajax({
		url:"scripts/proxyBlog.cgi",
		type:"get",
		data:"method=getPost&author=" + author + "&type=json",
		dataType:"json",
		success:function(data) {
			for(var i in data.BlogList) {
				var blog = data.BlogList[i];
				var date_time = blog.updated.split('T');
				var date = date_time[0];
				var time = date_time[1].split('.')[0];
				$("#content").prepend(
						"<div class='post' id='" + blog.id + "'>" +
						"<div class='post_meta'>" +
						"<p class='author'>Author<span>" + blog.author + "</span></p>" +
						"<p class='date'>Date<span>" + date + "</span><span>" + time + "</span></p>" +
						"</div><div class='post_content'><h2 class='title'>" + blog.title + "</h2>" +
						"<p class='content'>" + blog.content + "</p>" +
						"<div class='post_function'><a href='' class='edit_btn'>Edit</a><a href='' class='delete_btn'>Delete</a></div>" +
						"<div class='cleaner'></div></div><div class='cleaner'></div>"	// post id in href of edit and delete
						);
			}
		},
		error:function(h) {
			alert("Error in retrieve blogs");
		}
	});
}

/**
 * Method of handling post editing
 * @method editPost
 */
function editPost() {
	
}

/**
 * Method of handling post deletion
 * @method deletePost
 */
function deletePost() {
	
}

/**
 * Method of handling post creation
 * @method createPost
 */
function createPost() {
	$("#form").submit(function() {
		if( validateForm == true ) {
			$.ajax({
				url:"scripts/proxyBlog.cgi",
				type:"get",
				data:"method=createPost&title=" + $("#title").val() + "&content=" + $("#content").val(),
				success: function() {
					
				},
				error: function() {
					
				}
			});
		}
		else {
			return false;
		}
	});
}

/**
 * Method that change the menu presentation and content display when click the menu
 * @method changeMenu
 */
function changeMenu() {
	$("#create").click(function () {
		$("#home").removeClass("current");
		$("#create").addClass("current");
		$("#blog_content").hide();
		$("#create_form").show();
		return false;
	});
	$("#home").click(function () {
		$("#home").addClass("current");
		$("#create").removeClass("current");
		$("#blog_content").show();
		$("#create_form").hide();
		return false;
	});
	$("#api").click(function() {
		return false;
	});
	
}

/**
 * Method of handling form validation
 * @method validateForm
 * @return {Boolean}result true if form is validated
 */
function validateForm() {
	var msg = "";
	var result = true;
	
	if( !$("#title").val() ) {
		msg += "Title cannot be empty.\n";
	}
	if( !$("#content").val() ) {
		msg += "Content cannot be empty.\n";
	}
	if( !msg ) {
		alert(msg);
		result = false;
	}
	return result;
}

/**
 * To edit content of a blog entry
 * triggered when a Edit button is clicked
 * @method popupEdit
 */
function popupEdit() {
	// add on click
	
	// set mask height and width to fill up the window
	var maskH = $(document).height();
	var maskW = $(document).width();
	$("#mask").css({"width":maskW, "height":maskH});
	
	// transition effect
	$("#mask").fadeIn(1000).fadeTo("slow", 0.8);
	
	// set the popup window to center
	var pwin = $("#edit_popup");
	pwin.css("top", $(window).height()/2 - pwin.height()/2);
	pwin.css("left", $(window).width()/2 - pwin.width()/2);
	
	pwin.fadeIn(2000);
}

/**
 * To close the popup edit window, triggered when submit or cancel button is clicked
 * @method closeEdit
 */
function closeEdit() {
	$("#mask, #edit_popup").hide();
}

/**
 * Method to be triggered when page is loaded
 * @method setup
 */
function setup() {
	getBlogs();
	getPosts("51158405");
	changeMenu();
	validateForm();
}

$(document).ready(setup);